<?php
/**
* Copyright (c) 2002-2007 BioWare Corp.
* http://www.bigdaddymonitor.org
*
* This file is part of the BigDaddy Monitoring
* program. It is free software distributed under
* the terms of the BSD License. It is ditributed
* in the hopes that it will be useful, but
* WITHOUT ANY WARRANTY; without event the implied
* warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE.
*
* The BSD License is available from:
* http://www.opensource.org/licenses/bsd-license.php
**/
/**
 * Class::BigDaddy::BigDaddyData
 * Main classes for getting and displaying BigDaddy historical data
 * @author Duleepa Wijayawardhana <dups@bioware.com>
 * @version 30/05/2006 11:38:06
 */
class BigdaddyHistory extends BigDaddy
{
    /**
     * Constructor
     */
    public function __construct()
    {
        
    }
    /**
     * GetHistoryRange
     *
     * Returns the first and last monitor for each module during a particular monitoring period
     * 
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version Thu Oct 19 15:17:48 2006
     * @param int $nServerModuleID
     * @param int $nStartTime UNIX_TIMESTAMP
     * @param int $nEndTime UNIX_TIMESTAMP
     * @return array
     */
    public function GetHistoryRange($nServerModuleID, $nStartTime, $sSummaryType, $nEndTime = NULL)
    {
        if ($nEndTime == NULL)
        {
            $nEndTime = time();
        }
        
        switch ($sSummaryType)
        {
            case "ma15":
                $sTable = "ma15_server_module_history";
                break;
            case "ma60":
                $sTable = "ma60_server_module_history";
                break;
            case "ma180":
                $sTable = "ma180_server_module_history";
                break;
            case "bool":
                $sTable = "bool_server_module_history";
                break;
            case "":
            default:
                $sTable = "server_module_history";
                break;
        }
        
        //First Monitor Time
        $sql = "SELECT mon_lastreport
            FROM $sTable
            WHERE serverModuleID = $nServerModuleID
                AND mon_lastreport BETWEEN $nStartTime AND $nEndTime
            ORDER BY mon_lastreport ASC LIMIT 1";
        $aResult = db_fetch_result( db_query( $sql, array("dbHandle" => "bigdaddystats") ) );
        $nFirstMonitor = $aResult["mon_lastreport"];
        
        //Last Monitor Time
        $sql = "SELECT mon_lastreport
            FROM $sTable
            WHERE serverModuleID = $nServerModuleID
                AND mon_lastreport BETWEEN $nStartTime AND $nEndTime
            ORDER BY mon_lastreport DESC LIMIT 1";
        $aResult = db_fetch_result( db_query( $sql, array("dbHandle" => "bigdaddystats") ) );
        $nLastMonitor = $aResult["mon_lastreport"];
        
        $aReturn["firstmonitor"] = $nFirstMonitor;
        $aReturn["lastmonitor"] = $nLastMonitor;
        
        return $aReturn;
        
    }
    /**
     * GetNonBooleanHistoryByTime
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version Thu Oct 19 15:17:48 2006
     * @param int $nServerModuleID
     * @param int $nStartTime UNIX_TIMESTAMP
     * @param int $nEndTime UNIX_TIMESTAMP
     * @return array
     */
    public function GetNonBooleanHistoryByTime($nServerModuleID, $nStartTime, $sSummaryType = NULL, $nEndTime = NULL)
    {
        if ($nEndTime == NULL)
        {
            $nEndTime = time();
        }
        
        switch ($sSummaryType)
        {
            case "ma15":
                $sTable = "ma15_server_module_history";
                break;
            case "ma60":
                $sTable = "ma60_server_module_history";
                break;
            case "ma180":
                $sTable = "ma180_server_module_history";
                break;
            case "nonbool":
                $sTable = "nonbool_server_module_history";
                $bGetFullHistory = true;
                $nEndTime = $nEndTime - 1000;
                break;
            case "":
            default:
                $sTable = "server_module_history";
                break;
        }
        
        if ($bGetFullHistory)
        {
            $sql = "SELECT lastreportData, monHourStamp
            FROM $sTable
            WHERE serverModuleID = $nServerModuleID
                AND monHourStamp BETWEEN $nStartTime AND $nEndTime
            ORDER BY monHourStamp ASC";
            $aResult = db_fetch_array( db_query( $sql, array("dbHandle" => "bigdaddystats") ) );
            
            $i=0;
            if (is_array($aResult))
            {
                foreach ($aResult as $aRow)
                {
                    $aData = explode(";", $aRow["lastreportData"]);
                    
                    if (is_array($aData))
                    {
                        foreach ($aData as $sData)
                        {
                            preg_match("/(.*?):(.*)/", $sData, $aMatches);
                            $aReturn[$i]["mon_lastreport"] = $aRow["monHourStamp"]+$aMatches[1];
                            $aReturn[$i]["lastreport"] = $aMatches[2];
                            $i++;
                        }
                    }
                }
            }
            
            if (is_array($aReturn))
            {
                return $aReturn;
            }
            
            
        }
        else
        {
            $sql = "SELECT lastreport, mon_lastreport
                FROM $sTable
                WHERE serverModuleID = $nServerModuleID
                    AND mon_lastreport BETWEEN $nStartTime AND $nEndTime
                ORDER BY mon_lastreport ASC";
            $aResult = db_fetch_array( db_query( $sql, array("dbHandle" => "bigdaddystats") ) );
        
            if ( is_array($aResult) )
            {
                return $aResult;
            }
        }

        return;
    }
    /**
     * GetBooleanHistoryByTime
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version Thu Oct 19 15:17:48 2006
     * @param int $nServerModuleID
     * @param int $nStartTime UNIX_TIMESTAMP
     * @param int $nEndTime UNIX_TIMESTAMP
     * @return array
     */
    public function GetBooleanHistoryByTime($nServerModuleID, $nStartTime, $sSummaryType = NULL, $nEndTime = NULL, $bNoRawValues= NULL)
    {
        if ($nEndTime == NULL)
        {
            $nEndTime = time();
        }
        
        switch ($sSummaryType)
        {
            case "bool15":
                $nIncrement = 2;
                $sTable = "bool15_server_module_history";
                break;
            case "bool60":
                $nIncrement = 2;
                $sTable = "bool60_server_module_history";
                break;
            case "bool180":
                $nIncrement = 2;
                $sTable = "bool180_server_module_history";
                break;
            case "":
            default:
                $nIncrement = 15;
                $sTable = "server_module_history";
                break;
        }
        
        $sql = "SELECT lastreport, mon_lastreport
            FROM $sTable
            WHERE serverModuleID = $nServerModuleID
                AND mon_lastreport BETWEEN $nStartTime AND $nEndTime
            ORDER BY mon_lastreport ASC";
        $aResult = db_fetch_array( db_query( $sql, array("dbHandle" => "bigdaddystats") ) );
        
        if ( is_array($aResult) )
        {

            $nCurrentValue = 99;
            $i=0;
            $j=0;
            
            foreach ($aResult as $aRow)
            {
                if ($nCurrentValue != $aRow["lastreport"] )
                {
                    if (!$bNoRawValues)
                    {
                        $aReturn["values"][$i]["lastreport"] = $aRow["lastreport"];
                        $aReturn["values"][$i]["time"] = $aRow["mon_lastreport"];
                    }
                    if ($nCurrentValue != 99)
                    {
                        $aReturn["valuechange"][$nCurrentValue][]["end"] = $aRow["mon_lastreport"];
                        $aReturn["valuechange"][$aRow["lastreport"]][]["start"] = $aRow["mon_lastreport"];
                        //$aReturn["valuetime"][$nCurrentValue] = $aReturn["valuetime"][$nCurrentValue]+($aRow["mon_lastreport"] - $nCurrentTime);
                    }
                    else
                    {
                        $aReturn["monitoringstart"] = $aRow["mon_lastreport"];
                        $aReturn["valuechange"][$aRow["lastreport"]][]["start"] = $aRow["mon_lastreport"];
                        
                    }

                    $nCurrentValue = $aRow["lastreport"];
                    $i++;
                    
                }
                //Every nIncrement value (not 0)
                //and if it is the last value in the set
                else if (($j % $nIncrement == 0 && $j > 0))
                {
                    if (!$bNoRawValues)
                    {
                        $aReturn["values"][$i]["lastreport"] = $aRow["lastreport"];
                        $aReturn["values"][$i]["time"] = $aRow["mon_lastreport"];
                    }

                    $nCurrentValue = $aRow["lastreport"];
                    $i++;
                }
                else
                {
                    $nCurrentValue = $aRow["lastreport"];

                }
                $j++;                
                
            }
            if (!$bNoRawValues)
            {
                $aReturn["values"][$i]["lastreport"] = $aRow["lastreport"];
                $aReturn["values"][$i]["time"] = $aRow["mon_lastreport"];
            }
            $aReturn["valuechange"][$aRow["lastreport"]][]["end"] = $aRow["mon_lastreport"];
            $aReturn["monitoringend"] = $aRow["mon_lastreport"];
            
            
            //Before returning back lets total up the value change
            $aAllowedReport = array(1,0,-1,-2,-3);
            foreach ($aAllowedReport as $nReport)
            {
                $nTimeValue = 0;
                $nCurrentTime = 0;
                for ($i=0; $i<count($aReturn["valuechange"]["$nReport"]); $i++)
                {
                    foreach ($aReturn["valuechange"]["$nReport"][$i] as $sKey => $nTime)
                    {
                        if ($sKey == "start")
                        {
                            $nCurrentTime = $nTime;
                        }
                        
                        if ($sKey == "end")
                        {
                            $nTimeValue = $nTimeValue + ($nTime - $nCurrentTime);
                        }
                    }
                }
                
                $aReturn["valuetime"][$nReport] = $nTimeValue;
            }
            
            return $aReturn;
        }
        return;
    }
    /**
     * array GetLogEntriesByCronID($nID[, $nLimit])
     * returns an array of log entries for a particular cronID
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 16:30:04
     */
    public function GetLogEntriesByCronID($nID, $nLimit = NULL)
    {
        if ($nLimit == NULL)
        {
            $nLimit = 100;
        }
        
        $sql="SELECT logEntryTimestamp, logEntry
		FROM cronLog
		WHERE cronID='".$nID."'
		ORDER BY logEntryTimestamp DESC
		LIMIT $nLimit";
        $aResult = db_fetch_array(db_query($sql, array("dbHandle" => "bigdaddystats")));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
        
    }
    /**
     * array GetLogEntriesByCronID($nID[, $nLimit])
     * returns an array of log entries for a particular cronID
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 16:30:04
     */
    public function GetHistoryByCronID($nID, $nLimit = NULL)
    {
        if ($nLimit == NULL)
        {
            $nLimit = 100;
        }
        
        $sql="SELECT last_ranstart, last_ranend, time_elapsed
		FROM cronHistory
		WHERE cronID='".$nID."'
		ORDER BY last_ranend DESC
		LIMIT $nLimit";
        $aResult = db_fetch_array(db_query($sql, array("dbHandle" => "bigdaddystats")));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
        
    }
    
     
}
?>
